'use strict';
const db = uniCloud.database()
const dbCmd = db.command

exports.main = async (event, context) => {
	const { userId, articleId } = event
	//event为客户端上传的参数
	// console.log('event : ', event) 
	
	const userInfo = await db.collection('user').doc(userId).get()
	const article_likes_ids = userInfo.data[0].article_likes_ids
	let dbCmdFuns = null
	if (article_likes_ids.includes(articleId)) {
		dbCmdFuns = dbCmd.pull(articleId)
	} else {
		dbCmdFuns = dbCmd.addToSet(articleId)
	}

	await db.collection('user').doc(userId).update({
		article_likes_ids: dbCmdFuns
	})
	//event为客户端上传的参数
	console.log('event : ' + event)
	//返回数据给客户端
	return {
		code: 200,
		msg: '数据请求成功',
		data: userInfo.data[0]
	}
};
